Z.IsErrorRespuestaScrollXY = function(oFullResponse) {
    try {
        if (Y.lang.isBoolean(oFullResponse.success) == false) {
            alert("Se esperaba un boolean para oFullResponse.success");
            return true;
        }

        if (oFullResponse.redirect != undefined) {
            alert(oFullResponse.error);
            window.location = Z.ctx + oFullResponse.redirect;
            return true;
        }

        if (Y.lang.isString(oFullResponse.exception)) {
            Z.ShowException(oFullResponse.exception);
            return true;
        }

        Z.ProcesarDivs(oFullResponse);

        if (oFullResponse.success) {
            return false;
        } else {
            return true;
        }
    } catch (e) {
        alert("Z.IsErrorRespuestaScrollXY: " + e);
    }
};

Z.GetPaginatorScrollXY = function(containers, rowsPerPage) {

    return new Y.widget.Paginator({
        containers : containers,

        containerClass : "paginator-container-class",
        template : "{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink} ",

        firstPageLinkLabel : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
        firstPageLinkClass : "yui-pg-first", // default
        firstPageLinkTitle : "Primer Pagina", // default

        // Options for LastPageLink component
        lastPageLinkLabel : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
        lastPageLinkClass : "yui-pg-last", // default
        lastPageLinkTitle : "Ultima Pagina", // default

        // Options for PreviousPageLink component
        previousPageLinkLabel : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
        previousPageLinkClass : "yui-pg-previous", // default
        previousPageLinkTitle : "Pagina Previa", // default

        // Options for NextPageLink component
        nextPageLinkLabel : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
        nextPageLinkClass : "yui-pg-next", // default
        nextPageLinkTitle : "Pagina Siguiente", // default

        // Options for PageLinks component
        pageLinksContainerClass : 'yui-pg-pages', // default
        pageLinkClass : 'yui-pg-page', // default
        currentPageClass : 'yui-pg-current-page', // default

        pageReportTemplate : "Pag. {currentPage} de {totalPages}",

        rowsPerPage : rowsPerPage

    });
};

Z.TableScrollXY = function(config) {
    this.construir(config);

};

Z.TableScrollXY.prototype = {
    myDataTable : null,
    myDataSource : null,
    getDatatable : function() {
        return this.myDataTable;
    },
    construir : function(config) {

        var _URL = config.URL;
        var _FIELDS = config.FIELDS;
        var _COLUMNS_DEFS = config.COLUMNS;
        var _DATATABLE_DIV = config.DATATABLE_DIV;
        var _PAGINATOR_DIV = config.PAGINATOR_DIV;
        var _TOTAL_REGISTROS_DIV = config.TOTAL_REGISTROS_DIV;
        var _ROW_PER_PAGE = config.ROW_PER_PAGE;
        var _WIDTH = config.WIDTH;
        var _HEIGTH = config.HEIGTH;

        var _MSG_ERROR = '<font color="red">Error de Sistema.</font>';

        this.myDataSource = new Y.util.DataSource(_URL);
        this.myDataSource.responseType = Y.util.DataSource.TYPE_JSON;
        this.myDataSource.connXhrMode = "queueRequests";
        this.myDataSource.connMethodPost = true;
        this.myDataSource.maxCacheEntries = 0;
        this.myDataSource.responseSchema = {
            resultsList : "ResultSet.Result",
            fields : _FIELDS
        };

        this.myDataSource.doBeforeParseData = function(oRequest, oFullResponse, oCallback) {
            try {
                if (Z.IsErrorRespuestaScrollXY(oFullResponse)) {
                    return {};
                } else {
                    return oFullResponse;
                }
            } catch (e) {
                alert("doBeforeParseData: " + e);
                return {};
            }
        };

        this.myDataSource.subscribe('dataErrorEvent', function(oArgs) {

            try {

                oResponse = oArgs.response;

                if (false) {
                    alert("oResponse.error:" + oResponse.error);
                    alert("oResponse.tId:" + oResponse.tId);
                    alert("oResponse.status:" + oResponse.status);
                    alert("oResponse.statusText:" + oResponse.statusText);
                    alert("oResponse.getResponseHeader:" + oResponse.getResponseHeader);
                    alert("oResponse.getAllResponseHeaders:" + oResponse.getAllResponseHeaders);
                    alert("oResponse.responseText:" + oResponse.responseText);
                    alert("oResponse.responseXML:" + oResponse.responseXML);
                }
                var text = "";
                var flag = false;
                if (oResponse.status) {
                    text += oResponse.status + " ";
                    flag = true;
                }
                if (oResponse.statusText) {
                    text += oResponse.statusText + " ";
                    flag = true;
                }
                if (flag) {
                    this.myDataTable.set("MSG_ERROR", '<font color="red">Error de Sistema: ' + text + ' </font>');
                } else {
                    this.myDataTable.set("MSG_ERROR", _MSG_ERROR);
                }

            } catch (e) {
                alert("dataErrorEvent: " + e);
            }

        }, this, true);

        var myRowFormatter = function(elTr, oRecord) {
            if (oRecord.getData('modificado') == true) {
                Y.util.Dom.addClass(elTr, "modificado");
            }
            if (oRecord.getData('eliminado') == true) {
                Y.util.Dom.addClass(elTr, "eliminado");
            }
            if (oRecord.getData('nuevo') == true) {
                Y.util.Dom.addClass(elTr, "nuevo");
            }
            if (oRecord.getData('error') == true) {
                Y.util.Dom.addClass(elTr, "fontrojo");
            } else {
                Y.util.Dom.removeClass(elTr, "fontrojo");
            }

            return true;
        };

        var myPaginator = Z.GetPaginatorScrollXY(_PAGINATOR_DIV, _ROW_PER_PAGE);

        myPaginator.subscribe('totalRecordsChange', function(e) {

            myPaginator.fireEvent('pageChange');

        }, this, true);

        myPaginator.subscribe('pageChange', function(e) {

            try {
                var state = myPaginator.getState();
                var totalRecords = state.totalRecords;
                var start_index = state.records[0];
                var end_index = state.records[1];
                var container = document.getElementById(_TOTAL_REGISTROS_DIV);
                if (container) {
                    var texto = "Resultados ";
                    texto += start_index + 1;
                    texto += " - ";
                    texto += end_index + 1;
                    texto += " de ";
                    texto += totalRecords;
                    container.innerHTML = texto;
                }
            } catch (e) {

            }

        }, this, true);

        var oConfigs = {
            MSG_EMPTY : 'No existen registros.',
            MSG_LOADING : '<div style="color: blue;vertical-align:middle;"><img src="' + Z.zuictx + '/loader.gif" align="middle"/>&nbsp;Cargando...</div>',
            MSG_ERROR : '<font color="red">Error de Sistema.</font>',
            initialLoad : false,
            dynamicData : false,
            paginator : myPaginator,
            width:_WIDTH, 
            height:_HEIGTH

        };

        this.myDataTable = new Y.widget.ScrollingDataTable(_DATATABLE_DIV, _COLUMNS_DEFS, this.myDataSource, oConfigs);

        // Set up editing flow
        var highlightEditableCell = function(oArgs) {
            var elCell = oArgs.target;
            if (Y.util.Dom.hasClass(elCell, "yui-dt-editable")) {
                this.highlightCell(elCell);
            }
        };

        this.myDataTable.subscribe("cellMouseoverEvent", highlightEditableCell);
        this.myDataTable.subscribe("cellMouseoutEvent", this.myDataTable.onEventUnhighlightCell);
        this.myDataTable.subscribe("cellClickEvent", this.myDataTable.onEventShowCellEditor);

        // Subscribe to events for row selection
        this.myDataTable.subscribe("rowMouseoverEvent", this.myDataTable.onEventHighlightRow);
        this.myDataTable.subscribe("rowMouseoutEvent", this.myDataTable.onEventUnhighlightRow);
        //this.myDataTable.subscribe("rowClickEvent", this.myDataTable.onEventSelectRow);

        this.myDataTable.render();

        return this;

    },
    update : function(request) {

        var callbackObj = {
            success : function(sRequest, oResponse, oPayload) {
                var dtbl = this;
                dtbl.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
                var pag = dtbl.get('paginator');
                if (pag) {
                    pag.set('totalRecords', oResponse.results.length);
                }

                // myDataTable.set("sortedBy", null);
                // myDataTable.onDataReturnAppendRows.apply(myDataTable,
                // arguments);
            },
            failure : function(sRequest, oResponse, oPayload) {
                var dtbl = this;
                dtbl.deleteRows(dtbl.getRecordSet().getLength() - 1, -1 * dtbl.getRecordSet().getLength());
                dtbl.showTableMessage(Y.widget.DataTable.MSG_ERROR + " <br/> " + oResponse, Y.widget.DataTable.CLASS_ERROR);
                var pag = dtbl.get('paginator');
                if (pag) {
                    pag.set('totalRecords', 0);
                }
                // myDataTable.showTableMessage(Y.widget.DataTable.MSG_ERROR,
                // Y.widget.DataTable.CLASS_ERROR);
                // myDataTable.onDataReturnAppendRows.apply(myDataTable,
                // arguments);
            },
            scope : this.myDataTable
        };

        var requestLocal = "";
        requestLocal += "ddd=" + new Date().getTime();

        if (request) {
            requestLocal += request;
        }
        // alert('update: ' + requestLocal);

        this.myDataSource.sendRequest(requestLocal, callbackObj);
    },
    getJsonRecords : function() {
        try {
            var recordSet = this.myDataTable.getRecordSet();
            var arrayTemp = [];
            for (i = 0; i < recordSet.getLength(); i++) {
                var record = recordSet.getRecord(i);
                arrayTemp.push(record.getData());
            }
            var registros = Y.lang.JSON.stringify(arrayTemp);
            return registros;
        } catch (e) {
            alert("getRecords: " + e);
        }

    },

    clear : function() {
        try {
            var dtbl = this.myDataTable;
            dtbl.unselectAllRows();
            dtbl.deleteRows(dtbl.getRecordSet().getLength() - 1, -1 * dtbl.getRecordSet().getLength());
            var pag = dtbl.get('paginator');
            if (pag) {
                pag.set('totalRecords', 0);
            }
        } catch (x) {
            alert("clear: " + x.description);
        }
    }

};
